package cn.edu.scau.cmi.huangmaoqin.client;

import java.util.HashMap;

import javax.swing.JOptionPane;

import cn.edu.scau.cmi.huangmaoqin.composite.consistent.ConsistentOrganization;
import cn.edu.scau.cmi.huangmaoqin.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.huangmaoqin.composite.consistent.ConsistentTeam;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ConsistentCompositeClients extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("ConsistentComposite");
		primaryStage.setResizable(false);
		HashMap<TreeItem<String>, ConsistentTeam> teamMap = new HashMap<TreeItem<String>, ConsistentTeam>();
		// Node图标
		Node groupIcon = new ImageView(new Image(getClass().getResourceAsStream("group.png")));
		TreeItem<String> rootItem = new TreeItem<>("RootTeam", groupIcon);
		rootItem.setExpanded(true);
		ConsistentTeam rootTeam = new ConsistentTeam("RootTeam");
		teamMap.put(rootItem, rootTeam);

		TreeView<String> tree = new TreeView<>(rootItem);
		ScrollPane sPane = new ScrollPane(tree);

		FlowPane root = new FlowPane();
		root.getChildren().add(sPane);

		Button addPerson = new Button("添加个人");
		root.getChildren().add(addPerson);
		addPerson.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				TreeItem<String> selectedItem = tree.getSelectionModel().getSelectedItem();
				// 判断是否有选择节点并且选择的是否为团队
				if (selectedItem != null && teamMap.containsKey(selectedItem)) {
					String personName = JOptionPane.showInputDialog(null, "请输入个人的名称", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					if (personName == null) {
						return;
					} else if (personName.length() == 0) {
						JOptionPane.showMessageDialog(null, "没有输入个人名称，创建失败", "提示", JOptionPane.WARNING_MESSAGE);
					} else {

						ConsistentTeam consistentTeam = teamMap.get(selectedItem);
						if (consistentTeam.getChildren(personName) != null) {
							JOptionPane.showMessageDialog(null, "你所添加的个人已在团队中", "提示", JOptionPane.INFORMATION_MESSAGE);
							return;
						}
						Node personIcon = new ImageView(new Image(getClass().getResourceAsStream("person.png")));
						TreeItem<String> person = new TreeItem<>(personName, personIcon);
						selectedItem.getChildren().add(person);
						consistentTeam.addChildren(new ConsistentPerson(personName));
					}
				} else {
					JOptionPane.showMessageDialog(null, "请选择团队进行添加");
				}
			}
		});

		Button addGroup = new Button("添加团队");
		root.getChildren().add(addGroup);
		addGroup.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				// 得到选中节点
				TreeItem<String> selectedItem = tree.getSelectionModel().getSelectedItem();
				// 判断是否有选择节点并且选择的是否为团队
				if (selectedItem != null && teamMap.containsKey(selectedItem)) {
					String groupName = JOptionPane.showInputDialog(null, "请输入团队的名称", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					if (groupName == null) {
						return;
					} else if (groupName.length() == 0) {
						JOptionPane.showMessageDialog(null, "没有输入团队名称，创建失败", "提示", JOptionPane.WARNING_MESSAGE);
					} else {

						ConsistentTeam consistentTeam = teamMap.get(selectedItem);
						if (consistentTeam.getChildren(groupName) != null) {
							JOptionPane.showMessageDialog(null, "已存在该名称的个人或团队了，请重新换个名称吧", "提示",
									JOptionPane.INFORMATION_MESSAGE);
							return;
						}
						Node groupIcon = new ImageView(new Image(getClass().getResourceAsStream("group.png")));
						TreeItem<String> group = new TreeItem<>(groupName, groupIcon);
						selectedItem.getChildren().add(group);
						group.setExpanded(true);
						ConsistentTeam team = new ConsistentTeam(groupName);
						consistentTeam.addChildren(team);
						// 将团队添加到map里
						teamMap.put(group, team);
					}
				} else {
					JOptionPane.showMessageDialog(null, "请选择团队进行添加");
				}
			}
		});

		Button remove = new Button("移除");
		root.getChildren().add(remove);
		remove.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				// 得到选中节点
				TreeItem<String> selectedItem = tree.getSelectionModel().getSelectedItem();
				// 判断是否有选择节点
				if (selectedItem != null) {
					int isRemove = JOptionPane.showConfirmDialog(null, "是否确定要移除该个人/团体", "提示",
							JOptionPane.YES_NO_OPTION);
					if (isRemove == JOptionPane.YES_OPTION) {
						// 如果选中的为团体
						if (teamMap.get(selectedItem) != null) {
							ConsistentTeam selectTeam = teamMap.get(selectedItem);
							// 从Map里移除节点
							teamMap.remove(selectedItem);
							TreeItem<String> parent = selectedItem.getParent();
							// 从图中移除
							parent.getChildren().remove(selectedItem);
							ConsistentTeam lastTeam = teamMap.get(parent);
							// 从父亲那里去掉该节点对应的团队
							lastTeam.removeChild(selectTeam);
						} else {
							TreeItem<String> parent = selectedItem.getParent();
							// 从图中移除
							parent.getChildren().remove(selectedItem);
							ConsistentTeam lastTeam = teamMap.get(parent);
							// 从父亲那里去掉该节点对应的个人
							ConsistentOrganization toBeRemove = lastTeam.getChildren(selectedItem.getValue());
							lastTeam.removeChild(toBeRemove);
						}
					}
				} else {
					JOptionPane.showMessageDialog(null, "请选择要移除的个人或团队");
				}

			}

		});
		primaryStage.setScene(new Scene(root, 240, 420));
		primaryStage.show();
	}
}
